package com.gsit.tuneup.controller;

import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.gsit.tuneup.domain.Answer;
import com.gsit.tuneup.domain.Record;
import com.gsit.tuneup.domain.User;
import com.gsit.tuneup.repository.RecordRepository;
import com.gsit.tuneup.repository.UserRepository;
import com.gsit.tuneup.utils.Constant;


@Controller
@SessionAttributes(Constant.USER)
public class LoginController {
	
	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

	

	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private RecordRepository recordRepository;
	
	
	private void createRecords(){
		Record record = new Record(); 
		record.setName("first");
		record.setPath("ssss");
		Answer answer = new Answer();
		answer.setAmount(10.0);
		answer.setCurrency("INR");
		answer.setDate(new Date());
		answer.setName("first mr");
		answer.setRecord(record);
		record.setAnswer(answer);
		recordRepository.save(record);
	}
	
	
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String start(Locale locale) {
		return "redirect:/index";
	}
	
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(Locale locale, Model model) {
		model.addAttribute(Constant.USER, new User() );
		return "index";
	}
	
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(Locale locale, final HttpSession httpSession) {
		httpSession.invalidate();
		return "redirect:/";
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(@Valid User user,BindingResult result,final HttpSession httpSession) {
		String path = "index";
		/*if(user.getUserName().isEmpty()){
			model.addAttribute(Constant.USER, user );
			path= "index";
		}*/
		
		System.out.println("Server is called ....");
		
		User newUser = userRepository.findByUserNameAndPassword(user.getUserName(), user.getPassword());
		createRecords();
		if(newUser != null){
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			httpSession.setAttribute(Constant.USER, user);
			path="home";
		}
		
		/*else if(user.getUserName().equals("admin")&&user.getPassword().equals("admin")){
			logger.info("Welcome home! the client locale is "+ locale.toString());
			httpSession.setAttribute(Constant.USER, user);
			path="home";
		}*/
		
		return path;
	}
	
	
	
}
